﻿using System.Collections.Generic;
using MiniWeb.Core.Dtos;
using Newtonsoft.Json;
using SpeadFrm.Common;

namespace MiniWeb.Task
{
    public class SinaApi
    {
        private const string APP_Key = "3740601520";
        private const string APP_SCRE = "8c6f1e7a227b8c0a0c1b3bf009b5ee17";

        private const string DOMAIN = "https://api.weibo.com/oauth2";

        private const string CALLBACK_URL = "http://www.wasy.com/otherlogin/sinacallback";
        //：获取Authorization Code

        public static string GetLoginUrl()
        {
            string url = string.Format("{0}/authorize?client_id={1}&redirect_uri={2}&scope={3}", DOMAIN, APP_Key,
                                       CALLBACK_URL, "all");
            return url;
        }

        //通过Authorization Code获取Access Token

        public static SinaTokenJson GetAccessToken(string code)
        {
            string url =
                string.Format(
                    "{0}/access_token?", DOMAIN );

            Dictionary<string, string> dic = new Dictionary<string, string>
                                                 {
                                                     {"client_id",APP_Key},
                                                     {"client_secret",APP_SCRE},
                                                     {"grant_type","authorization_code"},
                                                     {"code",code},
                                                     {"redirect_uri",CALLBACK_URL},

                                                 };
            string returnvalue = WebUtils.DoPost(url, dic);
            SinaTokenJson info = JsonConvert.DeserializeObject<SinaTokenJson>(returnvalue);

            return info;

        }

    }

    public class SinaTokenJson
    {
        public string access_token { get; set; }

        public string expires_in { get; set; }

        public string uid { get; set; }
    }


}
